import java.io.File;
import java.math.BigDecimal;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;



public class Similar {
	String path;
	String []output;//mang luu ten do tuong dong giua 2 van ban
	Double []simi; //mang luu ket qua do tuong dong giua 2 van ban
	int k=3;//so cum nho nhat
	double alpha = 0.4;//nguong tuong dong
	int countTmp;//so luong anh xa
	Integer [] clustering;//van ban  thu i thuoc cum clustering[i]
	String []arr;// van ban thu i co ten arr[i]
	public Similar(){
		init();
	}
	//khoi tao
	public void init(){
		output  = new String [10000];
		simi = new Double [10000];
		countTmp=0;

	}
	public void setPath(String newPath){
		path = newPath;
	}
	//phan cum
	public void Divide(){

		File f = new File (path);
		File files[] = f.listFiles();
		int totalFile = files.length;
		arr= new String[totalFile];
		clustering = new Integer[totalFile];
		for(int i=0;i<totalFile;i++){
			clustering[i]=i;
			arr[i]= files[i].getName();
			//System.out.println(arr[i]);
		}
		
		
		while(fields()> k){//dung khi so cum nho hon k

			double maxSimilar=0.0;//do tuong dong cuc dai = gia tri nho nhat
			int page1=0,page2=0;
			for(int i=0;i<totalFile;i++){
				for(int j=i+1;j<totalFile;j++){
					if(clustering[i]!=clustering[j]){//neu 2 van ban dang khong cung 1 cum
						
						double distance=getSimilar(arr[i],arr[j]);//tinh do tuong dong giua 2 van ban thuoc 2 cum khac nhau
						
						//kiem tra xem do tuong dong co lon nhat tu truoc toi gio hay khong	
						if(distance > maxSimilar && clustering[i]!=clustering[j]){ 
							//luu do tuong dong cuc dai giua 2 cum
							maxSimilar= distance;
							page1=i;
							page2=j;
						}
					}
				}
			}
			//neu do tuong dong cuc dai giua 2 cum van ban tim duoc nho hon nguong thi dung
			if(maxSimilar < alpha)
				break;
			//sat nhap 2 cum tuong dong lai voi nhau
			for(int i=0;i<totalFile;i++){
				if(clustering[i]==clustering[page2] && i!=page2)
					clustering[i]=clustering[page1];
			}
			clustering[page2]=clustering[page1];
			//ghi ra man hinh ket qua
			// ket qua co dang: ten_van_ban:cum_cua_van_ban
			
			/*
			for(int e=0;e<totalFile;e++){
				System.out.print(arr[e]+":"+clustering[e]+" ");
			}
			*/
			
			System.out.println();
		}
		//in ra so cum hien tai

	}
	public double getSimilar(String s1, String s2){
		double result=0.0;
		String match= s1+" "+s2;
		for(int i=0;i<countTmp;i++){
			if(match.compareTo(output[i])==0){
				//System.out.println(match);
				//System.out.println(output[i]);
				return simi[i];
			}
		}
		return result;
		
	}
	public int fields(){
		Integer []check = new Integer[clustering.length];
		for(int i=0;i<clustering.length;++i){
			check[i]=0;
		}
		for(int i=0;i<clustering.length-1;++i){
			if(check[i]==0){
				
				for(int j=i+1;j<clustering.length;j++){
					if(clustering[i]==clustering[j])
						check[j]=1;
				}
			}
		}

		
	
		int result =0;
		for(int i=0;i<clustering.length;++i){
			if(check[i]==0)
				result++;
		}
		
		return result;
	}
	public void print(){
		
		int i=0;
		while(output[i]!=null)
		{
			System.out.println(output[i]);
			i++;
			
		}
		i=0;
		while(simi[i]!=null){
			System.out.println(simi[i]);
			i++;
		}
	}
	public void showListFile(){
		File f = new File (path);
		File files[] = f.listFiles();
		int totalFile = files.length;
		for(int i=0;i<totalFile;i++){
			System.out.println(arr[i]);
		}
	}
	public void write2Consol(){
		Integer []check = new Integer[clustering.length];
		for(int i=0;i<clustering.length;++i){
			check[i]=0;
		}
		int count =1;
		for(int i=0;i<clustering.length-1;++i){
			if(check[i]==0){
				String ipath = path +"\\"+ arr[i];
				System.out.println("\n\n*********************************************cluster number : "+count+"****************************************************");
				count ++;
				System.out.println("\t\t---------------------------------"+arr[i]+"---------------------------------");
				FileProcessing fp = new FileProcessing();
				System.out.println(fp.readFileNew(ipath));
				for(int j=i+1;j<clustering.length;j++){
					if(clustering[i]==clustering[j]){
						check[j]=1;
						String jpath = path +"\\"+ arr[j];
						System.out.println("\t\t---------------------------------"+arr[j]+"---------------------------------");
						FileProcessing jfp = new FileProcessing();
						System.out.println(jfp.readFileNew(jpath));
						}
				}
			}
		}
		
		
	}
	//tinh tat ca do tuong dong giua 2 van ban
	public void genSimilar(){
		init();
		File f = new File (path);
		File files[] = f.listFiles();
		int totalFile = files.length;
		int trong_so[] ={1, 1, 1, 1, 1, 1, 1, 1};
		for (int i1 = 0; i1 < totalFile; i1++) {
			for (int j = i1 + 1; j < totalFile; j++) {
				HashMap <String, String> perSon = new HashMap <String, String> (1);
				FileProcessing fp = new FileProcessing();
				String lines[]=fp.readFileNew(files[i1].getPath()).split("\n");
				CosineSimilarityForRs cs = new CosineSimilarityForRs();
				int length = lines.length;
				for (int i = 1; i < length - 1; i++)
					perSon.put(lines[i].split(":")[0].trim(), lines[i].split(":")[1].trim());
				HashMap <String, String> perSon1 = new HashMap <String, String> (1);
				lines=fp.readFileNew(files[j].getPath()).split("\n");
				length = lines.length;
				for (int i = 1; i < length-1; i++)
					perSon1.put(lines[i].split(":")[0].trim(), lines[i].split(":")[1].trim());
				Set senti = perSon.entrySet();
				Iterator iSenti = senti.iterator();
				int count = 0;
				double similar = 0.0;
				int sum = 0;
				while (iSenti.hasNext()) {
					Map.Entry<String, String> me = (Map.Entry<String, String>) iSenti.next();
					String pro = me.getValue();
					String pro1 = perSon1.get(me.getKey());
					if (pro != null && pro1!= null){
						similar += trong_so[count] * CosineSimilarityForRs.getSimilarity(pro, pro1, false);
					}
					sum += trong_so[count];
					count++;

				}
				//System.out.println("Similar:" + files[i1].getName() + "--" + files[j].getName() + BigDecimal.valueOf(similar/sum).setScale(3, BigDecimal.ROUND_HALF_UP).doubleValue());
				output[countTmp] = files[i1].getName() + " " + files[j].getName();
				if (!Double.isNaN(similar/sum))
					simi[countTmp] = BigDecimal.valueOf(similar/sum).setScale(3, BigDecimal.ROUND_HALF_UP).doubleValue();
				else{
					simi[countTmp]=0.0;
				}
				countTmp ++;
			}
		}
		for (int i = 0; i < countTmp; i++)
			for (int j = i+1; j < countTmp; j++) {
				if (simi[j] > simi[i]) {
					String tmp = output[j];
					output[j] = output[i];
					output[i] = tmp;
					double d = simi[j];
					simi[j] = simi[i];
					simi[i] = d;
					
				}
			}
		
	}
	public void showAllSimilar(){
		for (int i = 0; i < countTmp; i++) {
			System.out.println(output[i] +"\t" + simi[i]);
			
		}
		
	}
}
